// 文章首页
bangkeapp.controller('articalhomectrl', 
["$scope", "articalService", "$ionicContentBanner", "$interval", "$timeout", "$ionicLoading",
	function($scope, articalService, $ionicContentBanner, $interval, $timeout, $ionicLoading) {
		// 加载动画
		$ionicLoading.show({
			content: 'Loading',
			animation: 'fade-in',
			showBackdrop: true,
			maxWidth: 200,
			showDelay: 0
		});
		var flag = [false, false];
		$scope.artical = {};
		$scope.artical.coursetype = '1';
		
		// 获取专题
		articalService.articallist($scope.artical, 1, 10).then(function(result) {
			$scope.feature = result.Response;
			flag[0] = true;
			if (flag[0] && flag[1]) {
				$ionicLoading.hide();
			}
		}, function(error) {}, function(progress) {});
		
		// 获取公告
		$scope.artical.coursetype = '2';
		articalService.articallist($scope.artical, 1, 1).then(function(result) {
			if (undefined != result.Response && result.Response.length > 0) {
				$scope.notice = result.Response[0];
				$ionicContentBanner.showstablebanner({     
			       text : $scope.notice.coursename,     
			       type:'info',    
				});  
			}
			
			flag[1] = true;
			if (flag[0] && flag[1]) {
				$ionicLoading.hide();
			}
		}, function(error) {}, function(progress) {});
		
	}
]);
// 文章列表
bangkeapp.controller('articallistctrl', ["$scope", "$state", "$stateParams", "articalService", "$timeout",
	function($scope, $state, $stateParams, articalService, $timeout) {
		$scope.hasmore = true;
		$scope.page = 1;
		$scope.pagesize = 10;
		$scope.artical = {};
		$scope.artical.coursetype = $stateParams.coursetype;
		$scope.feature = [];

		$scope.getlist = function() {
			$timeout(function() {
				if(!$scope.hasmore) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					return;
				}
				// 获取文章列表
				articalService.articallist($scope.artical,
					$scope.page, $scope.pagesize).then(function(result) {

					angular.forEach(result.Response, function(value) {
						$scope.feature.push(value);
					});

					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.page++;

					$scope.total = result.No;
					if($scope.feature.length < $scope.total) {
						hasmore = true;
					} else {
						hasmore = false;
					}
				}, function(error) {}, function(progress) {});
			}, 500);
		}
		
		//控制列表是否允许其加载更多
		$scope.moreDataCanBeLoaded = function() {
			return $scope.hasmore;
		}
	}
]);
// 文章详情
bangkeapp.controller('articaldetailctrl', ["$scope", "$state", "$stateParams", "articalService",
	function($scope, $state, $stateParams, articalService) {
		$scope.artical = {};
		$scope.artical.id = $stateParams.id;
		// 获取详情
		articalService.articallist($scope.artical).then(function(result) {
			$scope.artical = result.Response[0];
			
			angular.element(document.getElementById("content"))[0].innerHTML = $scope.artical.content
		}, function(error) {}, function(progress) {});
		
		// 增加点击量
		articalService.clicknum($scope.artical).then(function(result) {
		}, function(error) {
		}, function(progress) {
		});
	}
]);